/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.infrastructure.rules;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * An abstract rule that allow AND, OR and NOT compositions of rules.
 * The CompositeRule uses AndRule, OrRule and InverseRule to concatenate rules.
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 */
public abstract class CompositeRule extends RuleBase {
    private List<IRule> components;

    public CompositeRule() {
        this.components = new ArrayList<IRule>();
    }

    public abstract boolean isValid();

    /**
     * Returns a readonly collection of concatenated rules.
     * @return the components
     */
    public List<IRule> getComponents() {
        return Collections.unmodifiableList(components);
    }

    /**
     * Concatenate this rule with other rule with AND composition.
     * @param other The other rule of the composition.
     * @return An AndRule containing this and other rules.
     */
    public AndRule and(IRule other) {
        return new AndRule(this, other);
    }

    /**
     * Concatenate this rule with other rule with OR composition.
     * @param other The other rule of the composition.
     * @return An OrRule containing this and other rules.
     */
    public OrRule or(IRule other) {
        return new OrRule(this, other);
    }

    /**
     * Inverse the validity of this rule
     * @return An InverseRule containing this rule.
     */
    public InverseRule not() {
        return new InverseRule(this);
    }

    /**
     * Add composite rules to the components collection.<BR/>
     * This is done automatically from the composite AND, OR and NOT rules.
     * @param childRule the rule to be added.
     */
    protected void addChildComponent(IRule childRule) {
        if (!this.components.contains(childRule)) {
            this.components.add(childRule);
        }
    }
}
